<?php

class ContentAction extends CommonAction {

    /**
     * 文章内容的路由
     * 
     * id > 0 && custom_flag == 1 (mode = 0)自定义页面
     * 
     * when custom_flag == 0 非自定义页面
     * id > 0 && count(pid) >  0  (mode = 1) 一级目录分类显示
     * id > 0 && count(pid) == 0  (mode = 2)目录文章显示
     * 
     * 使用文件包含的形式
     * 
     * */
    public function index(){
		if(isset($_REQUEST['id'])){
            //目录查找
            $id = $_REQUEST['id'];
			$where['id'] = $id;
			$Menu = M("Menu");
			$menu = $Menu->where($where)->find();
            if(!isset($menu)){
                $this->errorAjax('没有此目录');
            }
            //子目录查找
			$where = array();
			$where['pid'] = $id;
            $where['custom_flag'] = 0;
			$sub_count = $Menu->where($where)->count('id');
			
			
            if(1 == $menu['custom_flag']){ //自定义目录
                $this->assign("mode" , 0);
                $this->assign('custom_url' , $menu['custom_url']);
            }else if($sub_count > 0){ // 有子目录
				$this->assign("mode" , 1);
				$this->index_all($id);
			}else{ //无子目录
				$this->assign("mode" , 2);
                $this->index_list($id);
			}
            $this->display();
		}
        
    }
    
    /**
     * 所有子数据的概览
     * */
    public function index_all($id){
        $where['id'] = $id;
		$Menu = M("Menu");
		$menu = $Menu->where($where)->find();
        
        $where = array();
		$where['pid'] = $id;
        $where['custom_flag'] = 0;
		$submenu = $Menu->where($where)->select();
        
        $this->assign("name" , $menu['name']);
        $this->assign("submenu" , $submenu);
    }
    
    /**
     * 目录的文章列表
     * */
    public function index_list($id){
        $map = array();
        $map['menu_id'] = $id;
        if (isset ( $_REQUEST ['title'] ) && $_REQUEST ['title'] != ''){
            $map['title'] = array('like' , '%'.$_REQUEST ['title'].'%');
        }
        
		$model = D ('Content');
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
        
    }
    
    /**
     * 全文管理
     * */
    public function admin_content(){
        //列表过滤器，生成查询Map对象
		$map = $this->_search ();
        
		$model = D ('Content');
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
            //关闭了关系，但要加入目录名称
            $menu_map = M('Menu')->getField('id,name');
            $this->assign("menu_map" , $menu_map);
		}
		$this->display ();
    }
	
    
    
    /**
	 * 根据表单生成查询条件
	 * 进行列表过滤
	 */
	protected function _search() {
		//生成查询条件
		$model = D ('Content');
		$map = array ();
		foreach ( $model->getDbFields () as $key => $val ) {
			if (isset ( $_REQUEST [$val] ) && $_REQUEST [$val] != '') {
                //模糊查询
				$map [$val] = array('like' , '%'.$_REQUEST [$val].'%');
			}
		}
		return $map;

	}
    /**
	 * 根据表单生成查询条件
	 * 进行列表过滤
	 * @param Model $model 数据对象
	 * @param HashMap $map 过滤条件
	 */
	protected function _list($model, $map) {

		//取得满足条件的记录数
		$count = $model->where( $map )->count ( 'id' );
		if ($count > 0) {
		  
			import ( "@.ORG.Page" );
            
			$p = new Page ( $count, $listRows );
            
            
			//分页查询数据 ，根据时间排序,输出新文章
			$voList = $model->where($map)
                            ->order('id desc')
                            ->limit($p->firstRow . ',' . $p->listRows)
                            ->select();
            //echo $model->getlastsql();
            
			//分页跳转的时候保证查询条件
			foreach ( $map as $key => $val ) {
				if (! is_array ( $val )) {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}
            
			//分页显示
			$page = $p->show ();
			//模板赋值显示
			$this->assign ( 'list', $voList );
			$this->assign ( "page", $page );
            
		}
		$this->assign ( 'totalCount', $count );
		$this->assign ( 'numPerPage', C('PAGE_LISTROWS') );
		$this->assign ( 'currentPage', !empty($_REQUEST[C('VAR_PAGE')])?$_REQUEST[C('VAR_PAGE')]:1);
        
		return;
	}
    
    /**
     * dwz combox 二级目录
     * 返回 非规范json
     * exp.
     * [
     *  ['0' , 'name'],
     *  ['1' , 'name']
     * ]
     * */
    public function combox_menu_level_2(){
        $pid = $_REQUEST['id'];
        $where['pid'] = $pid;
        $where['level'] = 1;
        $where['custom_flag'] = 0; // 非自定义目录
        $list = M('Menu')->where($where)->order('id asc')->select();
        $json = '[';
        if(isset($pid) && $pid != 0 && !empty($list)){  
            
            $json = $json.'["0" , "请选择二级目录"]';
            foreach($list as $key => $module) {
                $json = $json.',["'.$module['id'].'","'.$module['name'].'"]';
            }
        }else{
            $json = $json.'["0" , "无"]';
        }
        $json = $json.']';
        echo $json;
        exit();
    }
    
    /**
     * 加入文章到数据库
     * */
    public function insert(){
        $data['title']        = $_REQUEST['title'];
        $data['author']       = $_REQUEST['author'];
        $data['update_time']  = time();
        $data['remark']       = $_REQUEST['remark'];
        $memu_level_1         = $_REQUEST['memu_level_1'];
        $memu_level_2         = $_REQUEST['memu_level_2'];
        //上传的字符串包含'\"'需要替换为'"'
        $data['article']      = str_replace('\"','"',$_REQUEST['article']);
        
        
        if(empty($memu_level_1)){ // 0也为空
            $this->errorAjax('请选择发布目录！');
        }else{
            $where['pid'] = $memu_level_1;
            $where['custom_flag'] = 0; // 非自定义目录
            $count = M('Menu')->where($where)->count();
            if($count != 0 && empty($memu_level_2)){
                $this->errorAjax('请选择“二级”发布目录！');
            }elseif($count == 0){ //只有一级目录
                $data['menu_id'] = $memu_level_1;
            }else{ //有二级目录
                $data['menu_id'] = $memu_level_2;
            }
            M('Content')->add($data);
            $this->successAjax('文章发布成功.');
        }
        
    }
    
    /**
     * 更新文章
     * */
    public function update(){
        $data['id']           = $_REQUEST['id'];
        $data['menu_id']      = $_REQUEST['menu_id'];
        $data['title']        = $_REQUEST['title'];
        $data['author']       = $_REQUEST['author'];
        $data['update_time']  = time();
        $data['remark']       = $_REQUEST['remark'];
        //上传的字符串包含'\"'需要替换为'"'
        $data['article']      = str_replace('\"','"',$_REQUEST['article']);
        M('Content')->save($data);
        $this->successAjax('文章修改成功.');
    }
    
    /**
     * 使用iframe盛放文章内容
     * */
	public function iframe_read(){
	   $id = $_REQUEST['id'];
       if(isset($id)){
            $this->assign('id' , $id);
            $this->display();
       }else{
            echo '没有文章，可能文章已经失效。';
       }
	   
	}
	
	public function read(){
	   $id = $_REQUEST['id'];
       if(isset($id)){
            $vo = M('Content')->find($id);
            $this->assign('vo' , $vo);
            $this->display();
       }else{
            echo '没有文章，可能文章已经失效。';
       }
       
	}
}